import sys
import PyQt5
from PyQt5.QtWidgets import QWidget, QPushButton, QLineEdit,QApplication, QMessageBox,QLabel, QGridLayout,QTextEdit
from PyQt5.QtCore import QCoreApplication
from sklearn.metrics.pairwise import cosine_similarity, pairwise_distances
class Example(QWidget):
     
    def __init__(self):
        super().__init__()
         
        self.initUI()
         
    def initUI(self):
        grid = QGridLayout()
        grid.setSpacing(4)
        
        #按钮
        qbtn = QPushButton('计算', self)
        qbtn.clicked.connect(self.jiexia)#按钮连接到方法
        qbtn.resize(qbtn.sizeHint())
        #qbtn.move(50, 50)#按钮放置位置
        grid.addWidget(qbtn,0,2)

        #住框体定义
        self.setGeometry(300, 300, 450, 150)
        self.setWindowTitle('余弦相似度')
        




        lbl1 = QLabel('向量1：', self)
        grid.addWidget(lbl1,0,0)
 
        lbl2 = QLabel('向量2：', self)
        grid.addWidget(lbl2,1,0)


        lbl3 = QLabel('余弦相似度：', self)
        grid.addWidget(lbl3,2,0)
 
        lbl4 = QLabel('余弦距离：', self)
        grid.addWidget(lbl4,3,0)

        

        self.QLineEdita=QLineEdit()
        grid.addWidget(self.QLineEdita,0,1)
        
        self.QLineEditb=QLineEdit()
        grid.addWidget(self.QLineEditb,1,1)

        self.QLineEditc=QLineEdit()
        grid.addWidget(self.QLineEditc,2,1)
        
        self.QLineEditd=QLineEdit()
        grid.addWidget(self.QLineEditd,3,1)
        #self.textbox=
        '''
        self.lbl5 = QLabel('余弦相似度', self)
        grid.addWidget(self.lbl5,2,1)
 
        self.lbl6 = QLabel('余弦距离', self)
        grid.addWidget(self.lbl6,3,1)
        '''
        self.setLayout(grid)
        self.show()
        


    def jiexia(self):
        print('1234')
       
       
        f=list(eval(self.QLineEdita.text()))
        
        print(f)
        
        d=list(eval(self.QLineEditb.text()))
        print(d)

        a=[f,d]
        print(a)
    
    
        #print(cosine_similarity(a))
        #print(round(cosine_similarity(a)[0,1],15))
        x=round(cosine_similarity(a)[0,1],15)
        
        self.QLineEditc.setText(str(x))
        #self.xlEdit3.insert(cosine_similarity(a)[0,1])
        #self.xlEdit4.setText(round(pairwise_distances(a)[0,1],15))
     
    #退出消息弹出框（messagebox）
    def closeEvent(self, event):
        reply = QMessageBox.question(self, '退出',
                "确定要退出吗?", QMessageBox.Yes |
                QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
             event.accept()
        else:
            event.ignore()
if __name__ == '__main__':
     
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())



    

